package DEEPERsource.DEEPERsource.source.machinelearning;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Map;

import machinelearning.wekawrapper.WekaClassifier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weka.classifiers.bayes.BayesNet;
@Deprecated
public class BayesNetClassifier extends WekaClassifier {
	private static Log _log = LogFactory.getLog(BayesNetClassifier.class);
	
	public BayesNetClassifier(){
		classifier = new BayesNet();
	}

	@Override
	public void buildClassifier() {
		try {
			classifier.buildClassifier(trainingSet);
		} catch (Exception e) {
			_log.error("Error building classifier");
			e.printStackTrace();
		}
	}

	@Override
	public void readClassifier(String modelFile) {
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(modelFile));
			classifier  = (BayesNet)in.readObject();
		} catch (FileNotFoundException e) {
			_log.error("Can't read the file");
			e.printStackTrace();
		} catch (IOException e) {
			_log.error("File read error");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			_log.error("Error reading classifier");
			e.printStackTrace();
		}
	}

	@Override
	/**
	 * Valid keys:
	 * priorProbabilityEstimator - double
	 */
	public Map<String, String> getOptions() {
		Map<String,String> result = new HashMap<String,String>();
		result.put("priorProbabilityEstimator",Double.toString(((BayesNet)classifier).getEstimator().getAlpha()));
		return result;
	}

	@Override
	/**
	 * Valid keys:
	 * priorProbabilityEstimator - double
	 */
	public void setOptions(Map<String, String> opt) {
		if(opt.containsKey("priorProbabilityEstimator")){
			((BayesNet)classifier).getEstimator().setAlpha(Double.parseDouble(opt.get("priorProbabilityEstimator")));
		}
	}
}
